﻿import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

plt.figure(figsize=(12, 6))

np.random.seed(42)

# 数据
m = 100
X = 6*np.random.rand(m, 1)-3 # 产生100个-3到3之间的随机数
y = 0.5*X**2+X+np.random.randn(m, 1)
X_new = np.linspace(-3, 3, 100).reshape(100, 1)

# 步骤：polynomial(多项式变换) => std(标准化) => fit(建模) => predict(预测)
for style, width, degree in (('r-', 3, 50), ('b--', 2, 2), ('g-+', 1, 1)):
    poly_features = PolynomialFeatures(degree=degree, include_bias=False)
    std = StandardScaler()
    lin_reg = LinearRegression()
    polynomial_reg = Pipeline([('poly_features', poly_features),
                               ('std', std),
                               ('lin_reg', lin_reg)])
    polynomial_reg.fit(X, y)
    y_new_2 = polynomial_reg.predict(X_new)
    plt.plot(X_new, y_new_2, style, label='degree ' + str(degree), linewidth=width)
plt.plot(X, y, 'b.')
plt.axis([-3, 3, -5, 10])
plt.legend()
plt.show()


'''
模型过于复杂容易过拟合，特征变换不易过于复杂
'''